package com.yun.baymax.activity.account;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.WindowManager;

import com.yun.baymax.R;
import com.yun.baymax.activity.BaseActivity;
import com.yun.baymax.activity.MainActivity;

import cn.bmob.v3.BmobUser;

/**
 * 欢迎/导航页(app启动Activity)
 * <p/>
 */
public class WelcomeActivity extends BaseActivity {
    private static boolean firstEnter = true;
    Handler handler = new Handler();
    private boolean isLogin;
    private boolean customSplash = false;
    private Runnable runnable;

    @Override
    protected int setLayoutResourceId() {
        return R.layout.activity_blank;
    }

    @Override
    protected void initVariables() {
        isLogin = BmobUser.getCurrentUser() != null;
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        //隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        if (savedInstanceState != null) {
            setIntent(new Intent()); // 从堆栈恢复，不再重复解析之前的intent
        }

        if (!firstEnter) {
            onIntent();
        } else {
            showSplashView();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (firstEnter) {
            firstEnter = false;
            runnable = new Runnable() {
                @Override
                public void run() {
                    onIntent();
                }
            };

            if (customSplash) {
                handler.postDelayed(runnable, 1000);
            } else {
                runnable.run();
            }
        }
    }

    @Override
    protected void loadData() {
//        CrashReport.testJavaCrash();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        /**
         * 如果Activity在，不会走到onCreate，而是onNewIntent，这时候需要setIntent
         * 场景：点击通知栏跳转到此，会收到Intent
         */
        setIntent(intent);
        onIntent();
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, 0);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.clear();
    }

    // 处理收到的Intent
    private void onIntent() {

        if (!isLogin) {
            goNext(null, LoginActivity.class);
            finish();
        } else {
            // 已经登录过了，处理过来的请求
            Intent intent = getIntent();
            if (intent != null) {
//                if (intent.hasExtra(NimIntent.EXTRA_NOTIFY_CONTENT)) {
//                    parseNotifyIntent(intent);
//                    return;
//                } else if (intent.hasExtra(Extras.EXTRA_JUMP_P2P) || intent.hasExtra(AVChatActivity.INTENT_ACTION_AVCHAT)) {
//                    parseNormalIntent(intent);
//                }
                parseNormalIntent(intent);
            } else showMainActivity();
        }
    }

    private void parseNotifyIntent(Intent intent) {
//        ArrayList<IMMessage> messages = (ArrayList<IMMessage>) intent.getSerializableExtra(NimIntent.EXTRA_NOTIFY_CONTENT);
//        if (messages == null || messages.size() > 1) {
//            showMainActivity(null);
//        } else {
//            showMainActivity(new Intent().putExtra(NimIntent.EXTRA_NOTIFY_CONTENT, messages.get(0)));
//        }
    }

    private void parseNormalIntent(Intent intent) {
        showMainActivity(intent);
    }

    private void showMainActivity() {
        showMainActivity(null);
    }

    private void showMainActivity(Intent intent) {
        goNext(null, MainActivity.class);
        finish();
    }

    private void showSplashView() {
        getWindow().setBackgroundDrawableResource(R.drawable.splash_bg);
        customSplash = true;
    }

    @Override
    protected void onStop() {
        super.onStop();
        handler.removeCallbacks(runnable);
    }
}
